#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#pragma comment(lib,"Winmm.lib")
#define n 10
#define high 600
#define width 350


int score;
int bird_x,bird_y;//小鸟位置
int bar_up_x[n],bar_up_y[n];//上挡板的位置
int bar_down_x[n],bar_down_y[n];//下挡板的位置
IMAGE img_bd1,img_bd2,img_bk,img_bar_up1,img_bar_up2,img_bar_down1,img_bar_down2;




void starup()//游戏初始化 
{
	int i;
	int k=1;//距离的倍数
	int l=1;//距离的倍数
	mciSendString("open E:\\background.mp3 alias bkmusic", NULL, 0, NULL);//播放背景音乐
	mciSendString("play bkmusic repeat", NULL, 0, NULL);	//循环播放
	
	
	initgraph(width,high);
	loadimage(&img_bk,"E:\\background.jpg");//读取图片到img对象中
	loadimage(&img_bd1,"E:\\bird1.jpg");	
	loadimage(&img_bd2,"E:\\bird2.jpg");
	loadimage(&img_bar_up1,"E:\\bar_up1.gif");
	loadimage(&img_bar_up2,"E:\\bar_up2.gif");
	loadimage(&img_bar_down1,"E:\\bar_down1.gif");
	loadimage(&img_bar_down2,"E:\\bar_down2.gif");
	
	bird_x=50;
	bird_y=200;
	
	bar_up_x[0]=300;
	bar_down_x[0]=300;
	bar_up_y[0]=-300;
	bar_down_y[0]=400;

	score=0;

	for(i=1;i<n;i++)
	{
		k++;
		bar_up_x[i]=bar_up_x[0]*k;
		bar_up_y[i]=-300-(rand()%100);
		
		bar_down_x[i]=bar_down_x[0]*k;
		bar_down_y[i]=400+(rand()%100);
	}
	
	BeginBatchDraw();
}




void show()
{
	int i,j;
	
	putimage(0,0,&img_bk); //显示背景
	for(i=0;i<n;i++)
	{
		putimage(bar_up_x[i],bar_up_y[i],&img_bar_up1,NOTSRCERASE);//显示上一半的障碍物
		putimage(bar_up_x[i],bar_up_y[i],&img_bar_up2,SRCINVERT);
	}
	
	
	for(j=0;j<n;j++)
	{
		putimage(bar_down_x[j],bar_down_y[j],&img_bar_down1,NOTSRCERASE);//显示下一半的障碍物
		putimage(bar_down_x[j],bar_down_y[j],&img_bar_down2,SRCINVERT);
	}
	putimage(bird_x,bird_y,&img_bd1,NOTSRCERASE);//显示小鸟
	putimage(bird_x,bird_y,&img_bd2,SRCINVERT);
	
	outtextxy(10,10,"得分：自己数！");
	/*char s[5];
	sprintf(s,"%d",score);
	outtextxy(50,10,score);*/

	FlushBatchDraw();
	Sleep(25);
}



void updataWithoutInput()//用户无关设置
{
	int i,j;

	for(i=0;i<n;i++)		//碰瓷而死
	{
		bar_up_x[i]=bar_up_x[i]-1;
		bar_down_x[i]=bar_down_x[i]-1; 
		
		if( (bird_x>bar_up_x[i]-30)&&(bird_x<bar_up_x[i]+140) )
		{
			if((bird_y<600+bar_up_y[i])  ||  (bird_y>bar_down_y[i]-15))
			{
				EndBatchDraw();
				
				setfillcolor(BLACK);
				solidrectangle(0,0,width,high);
				
				outtextxy(width/2-30,high/2,"游戏结束！");
				outtextxy(width/2-30,high/2+50,"按任意键退出");
				
				getch();
				closegraph();
			}
		}
	}

	for(j=0;j<n;j++)
	{
		if(bar_up_x[j]==50)
			score++;
	}

	
	if(bird_y>high-15)		//落入无底深渊die
	{
		EndBatchDraw();
		
		setfillcolor(BLACK);
		solidrectangle(0,0,width,high);
		
		outtextxy(width/2-30,high/2,"游戏结束！");
		outtextxy(width/2-30,high/2+50,"按任意键退出");
		
		getch();
		closegraph();
	}
	else
		bird_y=bird_y+3;
	
	
}




void updataWithInput()//用户有关设置
{
	char input;
	if(kbhit())//当按下键时执行
	{
		input=getch();
		if(input==' ' && bird_y>20)
		{
			bird_y=bird_y-60;
			
			mciSendString("close jpmusic", NULL, 0, NULL);//先把前面一次的音乐关闭
			mciSendString("open E:\\Jump.mp3 alias jpmusic", NULL, 0, NULL);//打开音乐
			mciSendString("play jpmusic", NULL, 0, NULL);//仅播放一次
		}
	} 
}

void gameover()
{
	EndBatchDraw();
	getch();
	closegraph();
}


void main()
{
	starup();//数据初始化
	while(1)//游戏循环 
	{
		show();//游戏显示 
		updataWithoutInput();//用户无关设置 
		updataWithInput(); //用户有关设置 
	}
	gameover();
} 
